package com.qf.rabbitmq.route;

import com.qf.rabbitmq.pojo.User;
import com.qf.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ConsumerFirst2 {

    private static final String EXCHANGENAME="bk2102EXCHANGE";
    private static final String QUEUENAME="queue";

    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionUtil.getConnection();
//        创建通道
        Channel channel = connection.createChannel();
//        声明交换机
        channel.exchangeDeclare(EXCHANGENAME, "direct");
//        声明队列
        channel.queueDeclare(QUEUENAME,true , false, false, null);
//        绑定交换机
        channel.queueBind(QUEUENAME, EXCHANGENAME, "haha");
//        等着收消息
        channel.basicConsume(QUEUENAME,true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(body);
                ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
                try {
                   User o = (User)objectInputStream.readObject();
                   System.out.println(o);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
